package com.jhd.fuzhou.view.task;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.ProcessService;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.PersonInfoVo;
import com.jhd.fuzhou.vo.SupervisionInspectionVo;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import static com.jhd.fuzhou.util.StartProcess.completeByAssign2;

/*审核任务*/
public class AuditTaskActivity extends BaseActivity {
    @BindView(R.id.rel_next_audit)RelativeLayout rel_next_audit;
    @BindView(R.id.tv_next_audit)TextView tv_next_audit;
    @BindView(R.id.lin_suggestion)LinearLayout lin_suggestion;
    @BindView(R.id.et_suggestion)EditText et_suggestion;
    private String data;
    private String message;//审核意见
    private String pass="1";
    private SupervisionInspectionVo supervisionInspectionVo;
    private Context context=this;
    private List<PersonInfoVo> personInfoVos;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_audit_task);
    }

    @Override
    protected void processLogic() {
        application.addTaskActivity(this);
        data=getIntent().getStringExtra("data");
        supervisionInspectionVo= JSON.parseObject(data, SupervisionInspectionVo.class);
        getTaskApproveLeve2();
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCancel();
            }
        });
    }
    @OnClick({R.id.btn_post,R.id.btn_cancel,R.id.rb_status_1,R.id.rb_status_2})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_post:
                HashMap map=new HashMap();
                map.put("pass",pass);
                if(pass.equals("2")){
                    if(StringUtil.isNullOrEmpty(et_suggestion.getText().toString().trim())) {
                        CustomToast.showText(context,"请输入审核意见!",3000,0,0).show();
                    }else{
                        completeByAssign2(application,(pass.equals("1")?personInfoVos.get(0).getUserName():supervisionInspectionVo.getCreateUser()),
                                supervisionInspectionVo.getTaskId(),et_suggestion.getText().toString(),map,supervisionInspectionVo.getId(),((pass.equals("1"))?"已审核":"已驳回"),"驳回成功！",context);
                    }
                }else{
                    if("请选择".equals(tv_next_audit.getText().toString())){
                        CustomToast.showText(context,"请选择下级审批人",3000,0,0).show();
                    }else{
                        completeByAssign2(application,(pass.equals("1")?personInfoVos.get(0).getUserName():supervisionInspectionVo.getCreateUser()),
                                supervisionInspectionVo.getTaskId(),et_suggestion.getText().toString(),map,supervisionInspectionVo.getId(),((pass.equals("1"))?"已审核":"已驳回"),"审核成功！",context);
                    }
                }
                break;
            case R.id.btn_cancel:
                onCancel();
                break;
            case R.id.rb_status_1:
                lin_suggestion.setVisibility(View.GONE);rel_next_audit.setVisibility(View.VISIBLE);
                pass="1";
                break;
            case R.id.rb_status_2:
                lin_suggestion.setVisibility(View.VISIBLE);rel_next_audit.setVisibility(View.GONE);
                pass="2";
                break;
        }
    }

    public void onCancel() {
        Intent intent = new Intent();
        intent.putExtra("position","2");
        ((Activity)context).setResult(RESULT_OK, intent);
        ((Activity)context).finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onCancel();
        }
        return super.onKeyDown(keyCode, event);
    }


    public void getTaskApproveLeve2(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        ProcessService vs = retrofit.create(ProcessService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("username",application.getName()).addParam("userId",application.getUserId());
        Call<JsonForResult> jsonForResultCall = vs.getTaskApproveLeve2(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            personInfoVos= JSON.parseArray(jsonForResult.getData().toString(), PersonInfoVo.class);
                            tv_next_audit.setText(personInfoVos.get(0).getName());
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
}